package com.liao.work;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

public class AdapterPhotoItemAdapter extends BaseAdapter {

    private List<String> urls;

    private Context context;
    private LayoutInflater layoutInflater;

    public AdapterPhotoItemAdapter(Context context, ArrayList<String> urls) {
        this.context = context;
        this.urls = urls;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return urls.size();
    }

    @Override
    public String getItem(int position) {
        return urls.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.adapter_photo_item, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((String) getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(String url, ViewHolder holder) {
        Picasso.get().load(url).into(holder.imageView);
    }

    protected class ViewHolder {
        private ImageView imageView;

        ViewHolder(View view) {
            imageView = (ImageView) view.findViewById(R.id.imageView);
        }
    }
}
